﻿package net.chrisjrock.text {
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author Christopher J. Rock
	 */
	public class LimitedLineTextField extends TextField {
		public var linesMax:uint;
		public var anchorLine:int;
		//public var limitToHeight:Boolean;
		
		public function LimitedLineTextField(linesMax:uint = 25 /*, limitToHeight:Boolean = true*/) {
			this.linesMax = linesMax;
			this.anchorLine = int.MAX_VALUE;
			//this.limitToHeight = limitToHeight;
		}
		
		protected function trimLines ():void {
			if (numLines > linesMax) super.text = text.slice(getLineOffset(numLines - linesMax) , length);
			scrollV = anchorLine;
			
			/*if (limitToHeight){
				var i0:int = numLines - 1;
				var lineHeightTotal:Number = 0;
				for (; i0 >= 0 ; i0--) {
					lineHeightTotal += getLineMetrics(i0).height;
					if (lineHeightTotal > height) {
						text = text.slice(getLineOffset(i0) , length);
						break;
					}
				}
			}*/
		}
		
		override public function appendText (newText:String):void {
			super.appendText(newText);
			//trimLines();
		}
		
		override public function replaceText (beginIndex:int , endIndex:int , newText:String):void {
			super.replaceText(beginIndex , endIndex , newText);
			trimLines();
		}
		
		override public function set text (value:String):void {
			super.text = value;
			trimLines();
		}
		
		override public function set htmlText (value:String):void {
			super.htmlText = value;
			trimLines();
		}
		
	}
	
}